// pages/jysj/jysj.js
const db = wx.cloud.database()
var util = require('../../utils/util.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        Name: '',
        SNumber: '',
        Class: '',
        College: '',
        Phone: '',
        show: false,
        checked: false,
        Borrow_id: '',
        text_id:'',
        book_name: '',
        book_writer: '',
        show2: false,
        Return_time: '',
        book_id: ''
    },
    showPopup() {
        this.setData({ show: true });
    },
    onClose() {
        this.setData({ show: false });
    },
    Borrow_books() {
        this.setData({ checked: true })
    },
    onInput(e) {
        if (e.detail.value === '') {
            console.log('error', '必填')
        }
    },
    onFrom() {
        if (this.isFromReady()) {
            this.handleSubmit();
        }
    },
    success() {
        setTimeout(() => {
            wx.switchTab({
                url: "../index/index"
            });
        }, 500);
    },
    handleSubmit() {
        wx.showLoading({
            title: '提交中...',
        })
        db.collection('apply_for').add({
            data: {
                Phone: this.data.Phone,
                Class: this.data.Class,
                Name: this.data.Name,
                College: this.data.College,
                SNumber: this.data.SNumber,
                apply_for_state: '0',
                apply_time: this.data.time,
                Return_time: this.data.Return_time,
                book_name: this.data.book_name,
                book_writer: this.data.book_writer,
                book_id: this.data.book_id
            }
        }).then(res => {
            wx.hideLoading()
            this.setData({ show2: true });
        })
    },
    isFromReady() {
        const children = this.selectAllComponents('.book-input')
        let count = 0;
        children.forEach(item => {
            if (item.isReady()) {
                count++;
            }
        })
        return count === children.length
    },
    //获取书籍数据
    getbookList(){
        db.collection("books_list").doc(this.data.Borrow_id).get().then(res => {
            this.setData({
                book_name: res.data.book_name,
                book_writer: res.data.book_writer,
                book_id: res.data._id
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            Borrow_id: options.Borrow_id
        })
        this.getbookList();
        const date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 2;
        let day = date.getDate();
        console.log(year, month, day)
        this.setData({
            Return_time: year + '-' + month + '-' + day
        })
        var time = util.formatTime(new Date());
        // 再通过setData更改Page()里面的data，动态更新页面的数据
        this.setData({
            time: time,
        });
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})